package com.wh.org.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wh.basic.utils.ExcelUtils;
import com.wh.basic.vo.AjaxResult;
import com.wh.org.dto.ShopDto;
import com.wh.org.pojo.Shop;
import com.wh.org.pojo.ShopAuditLog;
import com.wh.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService service;

    @PutMapping
    public AjaxResult addOrUpd(@RequestBody Shop shop, Model model) {
        try {
            if (shop.getId() != null) {
                service.upd(shop.getId());
                return AjaxResult.me();
            } else {
                service.add(shop);
                return AjaxResult.me();
            }
        } catch (Exception ex) {
            return AjaxResult.me().setSuccess(false).setMessage("出现异常");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        service.del(id);
        return AjaxResult.me();
    }

    @GetMapping
    public List<Shop> findAll() {
        return service.findAll();
    }

    @PatchMapping
    public AjaxResult patchDel(Long[] ids) {
        service.patchDel(ids);
        return AjaxResult.me();
    }

    @GetMapping("/{id}")
    public Shop findById(@PathVariable("id") Long id) {
        return service.findById(id);
    }

    @PostMapping
    public IPage<Shop> fingByPage(@RequestBody ShopDto shopDto) {

        IPage<Shop> byPage = service.findByPage(shopDto);
        System.out.println(byPage);
        return byPage;
    }

    //    点击入驻按钮的业务逻辑
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        service.settlement(shop);
        return AjaxResult.me();
    }

    /**
     * 驳回店铺入驻请求
     */
    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog) {
        service.auditReject(shopAuditLog);
        return AjaxResult.me();
    }


    /**
     * 店铺通过
     *
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog) {
        service.auditPass(shopAuditLog);
        return AjaxResult.me();
    }

    /**
     * 后台生成excel 并下载
     *
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        //ExcelUtils.exportExcel(导出的数据, 标题, 页名称, 导出的类型, 文件名, 响应对象)
        //文件名如果不写后缀默认是xls，可以写上xlsx就是指定的xlsx类型
        List<Shop> list = service.findAll();
        //调用excel工具类
        ExcelUtils.exportExcel(list, null, "店铺信息", Shop.class, "shop", response);
    }

    @GetMapping("importExcel")
    public AjaxResult importExcel(@RequestPart("file") MultipartFile multipartFile) {
        //ExcelUtils.importExcel(文件对象, 标题行数, 表头行数, 生成的对象类型)
        List<Shop> list = ExcelUtils.importExcel(multipartFile, 1, 1, Shop.class);
        for (Shop shop : list) {
            System.out.println(shop);
        }

        //保存到数据库中[自己实现，提示：动态sql 批量插入]
        return AjaxResult.me();

    }


}
